﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using D3;
using D3.Pathing;
using KCBF.Modules.AI.TreeSharp;
using NYTBD;
using NYTBD.Modules;
using Action = KCBF.Modules.AI.TreeSharp.Action;

namespace KCBF.Modules.AI
{
    class MoveToNpc : Decorator
    {
        public const string PathProperty = "path";
        public const string NextMoveProperty = "next_move";

        private Path _path;
        private Point _cur;
        private string _target = null;


        private bool _isFinnished = false;


        public MoveToNpc(string target) : base(null,null)
        {
            _target = target;
            _cur = new Point(int.MaxValue,int.MaxValue);

            Runner = context =>
                         {
                             if (_isFinnished)
                                 return false;
                             Unit u = _Game.GetUnit(target);
                             if (u == null)
                                 return false;
                             if (Squanto.GetDistance(u.X, u.Y) < 3) return false;
                             return true;
                         };

            var myChild = new PrioritySelector();

            myChild.AddChild(new Decorator(rem => _path == null,
                new Action(delegate
                               {
                                   Unit u = _Game.GetUnit(target);

                                   _path = Multiverse.GetCurrentMap().ComputePath(u.X, u.Y);

                                   var tmp = _path.GetNextNode();
                                   var tmpP = _path.MapToD3Coords(tmp.X, tmp.Y);

                                   _cur = new Point((int) tmpP[0], (int) tmpP[1]);
                                  
                                   Game.Print("New Paths");
                                   return RunStatus.Success;
                                   
                               })));

            myChild.AddChild(new Decorator(ret => Squanto.GetDistance(_cur.X, _cur.Y) > Squanto.MinDistDefault,
               new Sequence(
                   new Action(delegate
                                  {
                                      
                                      Squanto.WalkAtomic(_cur.X, _cur.Y);
                                      return RunStatus.Success;
                                  }),
                  new WaitContinue(50,rem=> false, new ActionAlwaysFail())
               )));

            myChild.AddChild(new Action(delegate
                                    {

                                        if (_path.Count() == 0)
                                        {
                                            Game.Print("Path finnished");
                                            _path = null;
                                            _isFinnished = true;
                                            return (RunStatus.Failure);
                                        } 
                                     
                                        var tmp = _path.GetNextNode();
                                        var tmpP = _path.MapToD3Coords(tmp.X, tmp.Y);

                                        _cur = new Point((int) tmpP[0], (int) tmpP[1]);
                                        
                                        return (RunStatus.Success);
                                    }));

            Children[0] = myChild;
        }

    }
}
